<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\member\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\member\model\MemberStatus as MemberStatusModel;
use app\member\model\Member as MemberModel;
use think\Db;
/**             
 * 行为管理控制器
 * @package app\admin\controller
 */
class Member extends Admin
{
    public function index()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $member_status=MemberStatusModel::getStatusMember();
        $sex=MemberStatusModel::getSex();
        $is_enable=MemberStatusModel::getIsEnable();
        $map = $this->getMap();
        $order = $this->getOrder('id desc');
        $from_time=input('date1');
        $to_time=input('date2');
        $search=input('search');
        $status_member=input('member_status');
        // 数据列表
        if(!empty($status_member) || $status_member=='0' || !empty($search) || !empty($from_time) || !empty($to_time)) {
            if ($from_time && $to_time) {
                $from_time =strtotime($from_time.' 00:00:00');
                $to_time = strtotime($to_time.' 23:59:59');
                $map['create_time'] = ['between', [$from_time, $to_time]];
            }
            if ($search) {
                $map["company|name|email"] = ['like', "%$search%"];
            }
            if ($status_member != '0') {
                $map['status'] = $status_member;
            }
            if ($status_member == '0') {
                $map['status'] = '0';
            }
        }
        $map['parentid']=0;
        if($map){
            $data_list = Db::name('member')->where($map)->order($order)->paginate();
        }else{
            $where['parentid']=0;
            $data_list = Db::name('member')->order($order)->where($where)->paginate();
        }
        // 分页数据
        $page = $data_list->render();
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('mix')
            ->setPageTitle('筛选')
            ->addText('search','搜索条件','',$search)
            ->addSelect('member_status', '审核状态', '', $member_status,$status_member)
            ->addDaterange('date1,date2', '日期范围')
            ->addBtn('<button type="submit" class="btn  btn-primary btn-rounded">确定搜索</button>')
            ->hideBtn('submit,back')
            ->layout(['search'=>2,'member_status'=>3,'date1'=>4,'date2'=>4,'form_group_btnGroup'=>3])
            ->setPageTitle('会员列表') // 设置页面标题
            ->setTablename('member') // 设置数据表名
            ->addOrder('balance') // 添加排序
            ->addColumns([ // 批量添加列
                ['head', '头像','picture','','','fix-table-td'],
                ['company', '公司名称','text.edit','','','fix-table-td'],
                ['name', '联系人','','','','fix-table-td'],
                ['sex', '性别','status','',$sex],
                ['email', '邮箱','text.edit','','','fix-table-td'],
                ['mobile', '手机号','text.edit','','','fix-table-td'],
                ['tel', '固定电话','text.edit','','','fix-table-td'],
                ['id_type', '证件类型','',''],
                ['balance', '会员余额','','','','fix-table-td'],
                ['status', '审核状态','status','',$member_status],
                ['is_enable', '是否启用','status','',$is_enable],
                ['remark', '审核备注','','','','fix-table-td'],
                ['right_button', '操作', 'btn']
            ])
            ->addValidate('Memberedit', 'company,email,mobile,tel') // 添加快捷编辑的验证器
            ->addFilter('is_enable',$is_enable)
            ->addFilter('sex',$sex)
            ->addFilter('id_type')
            ->addFilter('status',$member_status)
            ->addTopButtons('add') // 批量添加顶部按钮
            -> addTopButton('enable',['title'=>'启用','icon' => 'fa fa-fw fa-unlock','href' => url('isenable')])
            -> addTopButton('disable',['title'=>'禁用','icon'=>'fa fa-fw fa-unlock-alt'])
            ->addTopButton('add', ['title' => '导入', 'icon' => 'fa fa-fw fa-pencil-square-o', 'href'=> url('memberimport'), 'class' => 'btn btn-danger'], true)
            ->addTopButton('custom', ['title' => '下载导入模板', 'icon' => 'fa fa-fw fa-download', 'href'=> url('downloadexcel'), 'class' => 'btn btn-info'])
            ->addTopButton('custom', ['title' => '导出', 'icon' => 'fa fa-fw fa-download', 'href'=> url('export'), 'class' => 'btn btn-info js-get'])
            ->addRightButton('edit',['title'=>'审核','icon'=>'fa fa-fw fa-eyedropper','href'=>url('pending',['id' => '__id__'])])
            ->addRightButton('edit') // 批量添加右侧按钮
            ->addRightButton('edit',['title'=>'查看','icon'=>'fa fa-fw fa-file-text-o','href'=>url('look',['id' => '__id__'])])
            ->setRowList($data_list) // 设置表格数据
            ->isAjax(false)
            ->setFormMethod('get')
            ->setPages($page) // 设置分页数据
            ->fetch(); // 渲染页面
    }
    //银行
    public function bank()
    {
        cookie('__bank__', $_SERVER['REQUEST_URI']);
        $map = $this->getMap();
        // 数据列表
        $data_list = Db::name('member_settlement')->where($map)->order('id desc')->paginate();
        // 分页数据
        $page = $data_list->render();

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('会员银行信息')// 设置页面标题
            ->setTablename('member_settlement')// 设置数据表名
            ->setSearch(['bankname' => '银行名称','accountname'=>'银行开户名'])// 设置搜索参数
            ->addColumns([ // 批量添加列
                ['id', '银行编号'],
                ['memberid', '会员编号'],
                ['accountname', '银行开户名','','','','fix-table-td'],
                ['bankno', '公司银行账户','','','','fix-table-td'],
                ['bankname', '开户银行支行名称','','','','fix-table-td'],
                ['bankid', '支行联行号','','','','fix-table-td'],
                ['bandaddr', '开户银行所在地','','','','fix-table-td'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('add',['href' => url('add_bank')])
            ->addTopButton('delete',['href' => url('deletebanks')])// 批量添加顶部按钮
            ->addRightButton('edit',['href' => url('editbank',['id' => '__id__'])])// 批量添加右侧按钮
            ->addRightButton('delete',['icon' => 'fa fa-times', 'title' => '删除', 'href' => url('deletebank', ['id' => '__id__'])])
            ->setRowList($data_list)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch(); // 渲染页面
    }
    //新增银行信息
    public function add_bank(){
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data,'Bank');
            // 验证失败 输出错误信息
            if(true !== $result) {
                return $this->error($result);
            }else {
                $member=$data['memberid'][0];
                $data['memberid']=$member;
                $data['createtime']=time();
                $data['updatetime']=time();
                $ress=Db::name('member_settlement')->insert($data);
                if ($ress) {
                    return $this->success('新增成功', cookie('__bank__'));
                } else {
                    return $this->error('新增失败');
                }
            }
        }

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('新增银行信息') // 设置页面标题
            ->addSelect('memberid','选择会员','','','','ajax','',['url'=>'/admin.php/Shop/Shop/memberlist','datakey'=>'id','dataname'=>'name'])
            ->addFormItems([
                ['text', 'accountname', '银行开户名', ''],
                ['text', 'bankno', '公司银行账户',''],
                ['text', 'bankname', '开户银行支行名称', ''],
                ['text', 'bankid', '支行联行号', ''],
                ['text', 'bandaddr', '开户银行所在地', ''],
            ])
            ->layout(['accountname'=>4,'bankno'=>4,'bankname'=>4,'bankid'=>4,'bandaddr'=>4,'memberid'=>4])
            ->fetch();
    }
    //编辑银行信息
    public function editbank($id=''){

        $member_band = Db::name('member_settlement')->where('id',$id)->find();
        $memberinfo=Db::name("member")->where("id",$member_band['memberid'])->find();
        $arr = [];
        $arr[$memberinfo['id']] = $memberinfo['name'];
        $testid = $memberinfo['id'];
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();

            // 验证
            $result = $this->validate($data, 'Bank');
            // 验证失败 输出错误信息
            if(true !== $result){
                return $this->error($result);
            }else {
                if($member_band){
                    $member=$data['memberid'][0];
                    $data['memberid']=$member;
                    $data['updatetime']=time();
                    $res_bank = Db::name('member_settlement')->where('id', $id)->update($data);
                    if ($res_bank) {
                        return $this->success('编辑成功',cookie('__bank__'));
                    } else {
                        return $this->error('编辑失败');
                    }
                }
            }
        }

        return ZBuilder::make('form')
            ->setPageTitle('编辑银行信息') // 设置页面标题
            ->addFormItems([
                ['hidden','id'],
                ['select','memberid','选择会员','',$arr,$testid,'ajax','',['url'=>'/admin.php/Shop/Shop/memberlist','datakey'=>'id','dataname'=>'name']],
                ['text', 'accountname', '银行开户名', ''],
                ['text', 'bankno', '公司银行账户', ''],
                ['text', 'bankname', '开户银行支行名称', ''],
                ['text', 'bankid', '支行联行号', ''],
                ['text', 'bandaddr', '开户银行所在地', ''],
            ])
            ->layout(['accountname'=>4,'bankno'=>4,'bankname'=>4,'bankid'=>4,'bandaddr'=>4,'memberid'=>4])
            ->setFormData($member_band) // 设置表单数据
            ->fetch();
    }
    //会员新增
    public function add()
    {
        $params=Db::name('member_idtype')->where('typename','证件类型')->find();
        if($params){
            switch ($params['enteringtype']){
                case '0':
                    $data_list=explode(',',$params['paper']);
                    $data_list = array_flip($data_list);
                    foreach ($data_list as $k => $val) {
                        $data_list[$k] = $k;
                    }
                    $values[] = ['radio','id_type',$params['typename'],'',$data_list,'身份证'];
                    break;
                case '1':
                    $data_list=explode(',',$params['paper']);
                    $data_list = array_flip($data_list);
                    foreach ($data_list as $k => $val) {
                        $data_list[$k] = $k;
                    }
                    $values[] = ['checkbox','id_type',$params['typename'],'',$data_list];
                    break;
                case '2':
                    $values[] = ['text','id_type',$params['typename']];
                    break;
                case '3':
                    $data_list=explode(',',$params['paper']);
                    $data_list = array_flip($data_list);
                    foreach ($data_list as $k => $val) {
                        $data_list[$k] = $k;
                    }
                    $values[] = ['select','id_type',$params['typename'],'',$data_list];
                    break;
                case '4':
                    $values[] = ['image','id_type',$params['typename']];
                    break;
            }

        }
        /*;*/
        $list_province =Db::name("admin_area")->where("pid=0")->column("id,name");
        $list_grade=Db::name("member_level")->where('status=1')->column("id,name");
        $industry=Db::name('member_industry')->where('status=1')->column("id,name");
        $company_nature=Db::name('company_nature')->where('status=1')->column("id,company_nature");
        $department=Db::name('member_department')->where('status=1')->column("department_id,department");
        $sex=MemberStatusModel::getSex();
        $depot=MemberStatusModel::getDepot();
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data,'Member');
            // 验证失败 输出错误信息
            if(true !== $result) {
                return $this->error($result);
            }else{
                $res_province=Db::name("admin_area")->where("id=".$data['provinceid'])->find();
                $res_city=Db::name("admin_area")->where("id=".$data['cityid'])->find();
                $data['verify'] = genRandomString(6);
                $pass = MemberModel::encryption($data['password'], $data['verify']);
                $data['password'] = $pass;
                if(!empty($data['townid'])){
                    $res_town=Db::name("admin_area")->where("id=".$data['townid'])->find();
                    $data['town']=$res_town['name'];
                }
                $data['province']=$res_province['name'];
                $data['city']=$res_city['name'];
                if(empty($data['addr'])){
                    if(empty($res_town['name'])){
                        $data['addr']=$res_province['name'].$res_city['name'];
                    }else{
                        $data['addr']=$res_province['name'].$res_city['name'].$res_town['name'];
                    }
                }else{
                   if(strpos($data['addr'],'省') || strpos($data['addr'],'市')){
                           $data['addr']=$data['addr'];
                   }else{
                       if(empty($res_town['name'])){
                           $data['addr']=$res_province['name'].$res_city['name'].$data['addr'];
                       }else{
                           $data['addr']=$res_province['name'].$res_city['name'].$res_town['name'].$data['addr'];
                       }
                   }
                }
                $data['status']=0;
                $data['create_time']=time();
                $data['update_time']=time();
            }

            $res=Db::name('member')->insertGetid($data);
            session('id',$res);
            if ($res) {
                // 记录行为
                $details = "所属模块('member'),操作日志('会员新增'),新增会员id('$res')";
                action_log('member_add', 'member', $res, UID, $details);

                return $this->success('新增成功', url('member/Member/addbusinesslicence'));
            } else {
                return $this->error('新增失败');
            }

        }
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('新增') // 设置页面标题
            ->addFormItems([
                ['image', 'head', '会员头像', ''],
                ['image', 'idcard_face', '身份证正面照', ''],
                ['image', 'idcard_opposite', '身份证反面照', ''],
                ['image', 'hand_photo', '身份证手持照', ''],
                ['static', '', ''],
            ])
            ->addFormItems($values)
            ->addFormItems([ // 批量添加表单项
                ['radio', 'sex', '性别', '', $sex, 0],
                ['text', 'id_no', '证件号码'],
                ['text', 'user_name', '用户名', ''],
                ['text', 'mobile', '手机号'],
                ['text', 'name', '联系人', ''],
                ['text', 'l_mobile', '联系人手机号'],
                ['password', 'password', '密码', ''],
                ['text', 'email', '邮箱', ''],
                ['text', 'tel', '固话'],
                ['text', 'qq', 'qq'],
                ['text', 'wechat', '微信号'],
                ['date', 'birthday', '生日'],
                ['select', 'member_level', '会员等级','',$list_grade],
                ['static', '', ''],
                ['text', 'company', '公司名称'],
                ['select', 'company_nature', '公司性质','',$company_nature],
                ['select', 'industry', '公司行业','',$industry],
                ['select', 'department', '所在部门','',$department],
                ['radio', 'is_depot', '有无仓库', '', $depot, 0],
                ['static', '', ''],
                ['linkage', 'provinceid', '选择省份','',$list_province,'',url('get_city'),'cityid,townid'],
                ['linkage', 'cityid', '选择城市','','','',url('get_area'),'townid'],
                ['select', 'townid', '选择区域'],
                ['text', 'addr', '详细地址'],
            ])
            ->layout(['name'=>3,'email'=>3,'sex'=>3,'head'=>3,'idcard_face'=>3,'idcard_opposite'=>3,'hand_photo'=>3,'user_name'=>3,'l_mobile'=>3,
                'tel'=>3,'password'=>3,'mobile'=>3,'qq'=>3,'wechat'=>3,'birthday'=>3,'id_type'=>3,'id_no'=>3,'pay_points'=>3,'blance'=>3,'member_level'=>3,
                'provinceid'=>3,'cityid'=>3,'townid'=>3,'addr'=>3,'company'=>3,'industry'=>3,'company_nature'=>3,'department'=>3
            ])
            ->fetch();
    }
    //会员营业执照信息新增
    public function addbusinesslicence()
    {
        $id=session('id');
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data,'Business');
            // 验证失败 输出错误信息
            if(true !== $result) {
                return $this->error($result);
            }else{
                $data['typeid']=0;
                $data['createtime']=time();
                $data['updatetime']=time();
                $data['memberid']=$id;
                $res=Db::name('business_license')->insert($data);
                if ($res) {
                    return $this->success('新增成功', url('member/Member/addbank'));
                } else {
                    return $this->error('新增失败');
                }
            }

        }

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('新增证照信息') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['text', 'name', '法人代表姓名'],
                ['image', 'cardimg','法人代表身份证复印件'],
                ['image', 'shoppersoncodeimg','负责人身份证复印件'],
                ['static','',''],
                ['text', 'businesslicenseno', '营业执照号', ''],
                ['date', 'effectivetime', '营业执照有效期',''],
                ['image', 'effectiveimg', '营业执照副本图片'],
                ['static','',''],
                ['text', 'registeredcapital', '注册资本', ''],
                ['text', 'organizationcode', '组织机构代码'],
                ['image', 'organizationcodeimg','组织机构代码证复印件'],
                ['static','',''],
                ['text', 'generaltaxpayerproof', '一般纳税人说明'],
                ['text', 'taxcode', '纳税类型码'],
                ['text', 'taxno', '税务登记号码'],
                ['image', 'taximg', '税务登记证复印件'],
                ['textarea', 'scope', '经营范围', '']
            ])
            ->layout(['businesslicenseno'=>3,'effectivetime'=>3,'registeredcapital'=>3,
                'organizationcode'=>3,'generaltaxpayerproof'=>3,'name'=>3,'taxcode'=>3,'taxno'=>3,'effectiveimg'=>3,'taximg'=>3,'organizationcodeimg'=>3,'shoppersoncodeimg'=>3,'cardimg'=>3
            ])
            ->fetch();
    }

    //新增银行信息
    public function addbank(){
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data,'Bank');
            // 验证失败 输出错误信息
            if(true !== $result) {
                return $this->error($result);
            }else {
                $id=session('id');
                $data['memberid']=$id;
                $data['createtime']=time();
                $data['updatetime']=time();
                $ress=Db::name('member_settlement')->insert($data);
                if ($ress) {
                    // 记录行为
                    $details = "所属模块('member'),操作日志('会员银行信息新增'),会员id('$id')";
                    action_log('memberindustry_add', 'member', $id, UID, $details);
                    return $this->success('新增成功', url('member/Member/addbalance'));
                } else {
                    return $this->error('新增失败');
                }
            }
            }

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('新增会员银行信息') // 设置页面标题
            ->addFormItems([
                ['text', 'accountname', '银行开户名', ''],
                ['text', 'bankno', '公司银行账户',''],
                ['text', 'bankname', '开户银行支行名称', ''],
                ['text', 'bankid', '支行联行号', ''],
                ['text', 'bandaddr', '开户银行所在地', ''],
            ])
            ->layout(['accountname'=>4,'bankno'=>4,'bankname'=>4,'bankid'=>4,'bandaddr'=>4])
            ->fetch();
    }
    //新增开票信息
    public function addbalance(){
        // 保存数据
        $list_province =Db::name("admin_area")->where("pid=0")->column("id,name");
        $invoiceType=Db::name('invoice_type')->where('status=1')->column('invoice_id,invoice_type');
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data,'Balance');
            // 验证失败 输出错误信息
            if(true !== $result) {
                return $this->error($result);
            }else {
                $id=session('id');
                $data['memberid']=$id;
                $data['typeid']=2;
                $c_province=Db::name("admin_area")->where("id=".$data['c_provinceid'])->find();
                $c_city=Db::name("admin_area")->where("id=".$data['c_cityid'])->find();
                if(!empty($data['c_townid'])){
                    $c_town=Db::name("admin_area")->where("id=".$data['c_townid'])->find();
                }
                if(empty($data['bandaddr'])){
                    if(empty($c_town['name'])){
                        $data['bandaddr']=$c_province['name'].$c_city['name'];
                    }else{
                        $data['bandaddr']=$c_province['name'].$c_city['name'].$c_town['name'];
                    }
                }else{
                    if(strpos($data['bandaddr'],'省') || strpos($data['bandaddr'],'市')){
                        $data['bandaddr']=$data['bandaddr'];
                    }else{
                        if(empty($c_town['name'])){
                            $data['bandaddr']=$c_province['name'].$c_city['name'].$data['bandaddr'];
                        }else{
                            $data['bandaddr']=$c_province['name'].$c_city['name'].$c_town['name'].$data['bandaddr'];
                        }
                    }
                }
                $res_province=Db::name("admin_area")->where("id=".$data['provinceid'])->find();
                $res_city=Db::name("admin_area")->where("id=".$data['cityid'])->find();
                if(!empty($data['townid'])){
                    $res_town=Db::name("admin_area")->where("id=".$data['townid'])->find();
                }
                if(empty($data['addr'])){
                    if(empty($res_town['name'])){
                        $data['signup_address']=$res_province['name'].$res_city['name'];
                    }else{
                        $data['signup_address']=$res_province['name'].$res_city['name'].$res_town['name'];
                    }
                }else{
                    if(strpos($data['addr'],'省') || strpos($data['addr'],'市')){
                        $data['signup_address']=$data['addr'];
                    }else{
                        if(empty($res_town['name'])){
                            $data['signup_address']=$res_province['name'].$res_city['name'].$data['addr'];
                        }else{
                            $data['signup_address']=$res_province['name'].$res_city['name'].$res_town['name'].$data['addr'];
                        }
                    }
                }
                $data['createtime']=time();
                $data['updatetime']=time();
                $ress=Db::name('member_balance')->insert($data);
                if ($ress) {
                    unset($_SESSION['id']);
                    return $this->success('新增成功',cookie('__forward__'));
                } else {
                    return $this->error('新增失败');
                }
            }
        }
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('新增开票信息') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['select', 'invoice_type', '发票类型','',$invoiceType],
                ['text', 'company', '单位名称', ''],
                ['text', 'taxpayer_no', '纳税人识别号', ''],
                ['static', '', ''],
                ['image', 'taxpayerimg','一般纳税人资质'],
                ['image', 'addticketempowerimg','增票授权委托书'],
                ['image', 'businesschangeimg','工商变更证明照'],
                ['static', '', ''],
                ['text', 'tel', '电话', ''],
                ['text', 'accountname', '银行开户名', ''],
                ['text', 'bankno', '公司银行账户',''],
                ['text', 'bankname', '开户银行支行名称', ''],
                ['text', 'bankid', '支行联行号', ''],
                ['static', '', ''],
                ['linkage', 'c_provinceid', '选择省份','',$list_province,'',url('c_city'),'c_cityid,c_townid'],
                ['linkage', 'c_cityid', '选择城市','','','',url('c_area'),'c_townid'],
                ['select', 'c_townid', '选择区域'],
                ['text', 'bandaddr', '开户银行所在地', ''],
                ['linkage', 'provinceid', '选择省份','',$list_province,'',url('get_city'),'cityid,townid'],
                ['linkage', 'cityid', '选择城市','','','',url('get_area'),'townid'],
                ['select', 'townid', '选择区域'],
                ['text', 'addr', '注册地址', ''],
            ])
            ->layout(['accountname'=>3,'bankno'=>3,'bankname'=>3,'bankid'=>3,'bandaddr'=>3,'invoice_type'=>3,'taxpayerimg'=>3,'addticketempowerimg'=>3,'businesschangeimg'=>3,
            'company'=>3,'taxpayer_no'=>3,'tel'=>3,'provinceid'=>3,'cityid'=>3,'townid'=>3,'addr'=>3,'c_provinceid'=>3,'c_cityid'=>3,'c_townid'=>3])
            ->fetch();
    }
    //会员审核
    public function pending($id=''){
        $info = Db::name('member')->where('id',$id)->field('id,remark,status')->find();
        $member_status=MemberStatusModel::getStatusMember();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['update_time']=time();
            $res=Db::name('member')->update($data);
            if ($res) {
                return $this->success('操作成功',cookie('__forward__'));
            } else {
                return $this->error('操作失败');
            }
        }
        return ZBuilder::make('form')
            ->setPageTitle('会员审核') // 设置页面标题
            ->addFormItems([
                ['hidden','id'],
                ['textarea', 'remark', '审核备注', ''],
                ['radio', 'status', '审核状态', '', $member_status],
            ])
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }
    //会员编辑
    public function edit($id='',$group = 'tab1')
    {
        $params=Db::name('member_idtype')->where('typename','证件类型')->find();
        if($params){
            switch ($params['enteringtype']){
                case '0':
                    $data_list=explode(',',$params['paper']);
                    $data_list = array_flip($data_list);
                    foreach ($data_list as $k => $val) {
                        $data_list[$k] = $k;
                    }
                    $values[] = ['radio','id_type',$params['typename'],'',$data_list,'身份证'];
                    break;
                case '1':
                    $data_list=explode(',',$params['paper']);
                    $data_list = array_flip($data_list);
                    foreach ($data_list as $k => $val) {
                        $data_list[$k] = $k;
                    }
                    $values[] = ['checkbox','id_type',$params['typename'],'',$data_list];
                    break;
                case '2':
                    $values[] = ['text','id_type',$params['typename']];
                    break;
                case '3':
                    $data_list=explode(',',$params['paper']);
                    $data_list = array_flip($data_list);
                    foreach ($data_list as $k => $val) {
                        $data_list[$k] = $k;
                    }
                    $values[] = ['select','id_type',$params['typename'],'',$data_list];
                    break;
                case '4':
                    $values[] = ['image','id_type',$params['typename']];
                    break;
            }

        }
        $url=cookie('__forward__');
        $js = <<<EOF
            <script type="text/javascript">
              function back(){
              window.location.href="$url";
              }
            </script>
EOF;
        session('id',$id);
        $list_tab = [
            'tab1' => ['title' => '会员信息', 'url' => url('edit', ['group' => 'tab1'])],
            'tab3' => ['title' => '信用额度', 'url' => url('edit', ['group' => 'tab3'])],
            'tab4' => ['title' => '证照信息', 'url' => url('edit', ['group' => 'tab4'])],
//            'tab2' => ['title' => '银行信息', 'url' => url('edit', ['group' => 'tab2'])],
            'tab5' => ['title' => '开票信息', 'url' => url('edit', ['group' => 'tab5'])],
        ];
        switch ($group) {
            case 'tab1':
                $id=session('id');
                if ($id === null) return $this->error('缺少参数');
                // 获取数据(排除password)
                $info = Db::name('member')->where('id',$id)->field('password', true)->find();
                $list_province = db("admin_area")->where("pid=0")->column("id,name");
                $list_grade=Db::name("member_level")->where("status=1")->column("id,name");
                $industry=Db::name('member_industry')->where('status=1')->column("id,name");
                $company_nature=Db::name('company_nature')->where('status=1')->column("id,company_nature");
                $department=Db::name('member_department')->where('status=1')->column("department_id,department");
                $sex=MemberStatusModel::getSex();
                $depot=MemberStatusModel::getDepot();
                $status=MemberStatusModel::getStatusAttr($info['status']);
                $info['status']=$status;
                $is_enable=MemberStatusModel::getIsEnable();
                if(!empty($info['provinceid'])){
                    $list_city = db("admin_area")->where("pid",$info['provinceid'])->column("id,name");
                    $list_area = db("admin_area")->where("pid",$info['cityid'])->column("id,name");
                }else{
                    $list_city ='';
                    $list_area ='';
                }
                // 保存数据
                if ($this->request->isPost()) {
                    $data = $this->request->post();
                    // 验证
                    $result = $this->validate($data, 'Member.update');
                    // 验证失败 输出错误信息
                    if(true !== $result){
                        return $this->error($result);
                    }else{
                        $res_province=Db::name("admin_area")->where("id=".$data['provinceid'])->find();
                        $res_city=Db::name("admin_area")->where("id=".$data['cityid'])->find();
                        if(!empty($data['townid'])){
                            $res_town=Db::name("admin_area")->where("id=".$data['townid'])->find();
                            $data['town']=$res_town['name'];
                        }
                        $data['province']=$res_province['name'];
                        $data['city']=$res_city['name'];
                        if(empty($data['addr'])){
                            if(empty($res_town['name'])){
                                $data['addr']=$res_province['name'].$res_city['name'];
                            }else{
                                $data['addr']=$res_province['name'].$res_city['name'].$res_town['name'];
                            }
                        }else{
                            if(strpos($data['addr'],'省') || strpos($data['addr'],'市')){
                                $data['addr']=$data['addr'];
                            }else{
                                if(empty($res_town['name'])){
                                    $data['addr']=$res_province['name'].$res_city['name'].$data['addr'];
                                }else{
                                    $data['addr']=$res_province['name'].$res_city['name'].$res_town['name'].$data['addr'];
                                }
                            }
                        }
                        $data['update_time']=time();
                    }

                    // 如果没有填写密码，则不更新密码
                    if ($data['password'] == '') {
                        unset($data['password']);
                    }else{
                        $data['verify'] = genRandomString(6);
                        $pass = MemberModel::encryption($data['password'], $data['verify']);
                        $data['password'] = $pass;
                    }
                    $res=Db::name('member')->update($data);
                    if ($res) {
                        $details = "所属模块('member'),操作日志('会员编辑'),会员id('$id')";
                        action_log('member_edit', 'member', $id, UID, $details);
                        return $this->success('编辑成功');
                    } else {
                        return $this->error('编辑失败');
                    }
                }

                return ZBuilder::make('form')
                    ->setTabNav($list_tab,  $group)
                    ->addFormItems([
                        ['image', 'head', '会员头像', ''],
                        ['image', 'idcard_face', '身份证正面照', ''],
                        ['image', 'idcard_opposite', '身份证反面照', ''],
                        ['image', 'hand_photo', '身份证手持照', ''],
                        ['static', '', ''],
                    ])
                    ->addFormItems($values)
                    ->addFormItems([ // 批量添加表单项
                        ['hidden','id'],
                        ['radio', 'sex', '性别', '', $sex],
                        ['text', 'id_no', '证件号码'],
                        ['text', 'user_name', '用户名', ''],
                        ['text', 'mobile', '手机号'],
                        ['static', 'name', '联系人', ''],
                        ['text', 'l_mobile', '联系人手机号'],
                        ['password', 'password', '密码', ''],
                        ['text', 'email', '邮箱', ''],
                        ['text', 'tel', '固话'],
                        ['text', 'qq', 'qq'],
                        ['text', 'wechat', '微信号'],
                        ['date', 'birthday', '生日'],
                        ['select', 'member_level', '会员等级','',$list_grade],
                        ['static', '', ''],
                        ['text', 'company', '公司名称'],
                        ['select', 'company_nature', '公司性质','',$company_nature],
                        ['select', 'industry', '公司行业','',$industry],
                        ['select', 'department', '所在部门','',$department],
                        ['radio', 'is_depot', '有无仓库', '', $depot],
                        ['static', '', ''],
                        ['linkage', 'provinceid', '选择省份','',$list_province,'',url('get_city'),'cityid,townid'],
                        ['linkage', 'cityid', '选择城市','',$list_city,'',url('get_area'),'townid'],
                        ['select', 'townid', '选择区域','',$list_area],
                        ['text', 'addr', '详细地址'],
                        ['static', 'status', '审核状态'],
                        ['radio', 'is_enable', '是否启用', '', $is_enable]
                    ])
                    ->layout(['user_name'=>3,'name'=>3,'email'=>3,'sex'=>3,'creditlimit'=>3,'temporary_creditlimit'=>3,'account_period'=>3,'temporary_time'=>3,'temporary_endtime'=>3,
                        'tel'=>3,'password'=>3,'mobile'=>3,'qq'=>3,'wechat'=>3,'birthday'=>3,'id_type'=>3,'id_no'=>3,'pay_points'=>3,'blance'=>3,'member_level'=>3,'l_mobile'=>3,
                        'provinceid'=>2,'cityid'=>2,'townid'=>2,'addr'=>6,'company'=>3,'industry'=>3,'status'=>3,'is_enable'=>3,'head'=>3,'idcard_face'=>3,'idcard_opposite'=>3,'hand_photo'=>3,
                        'company_nature'=>3,'department'=>3
                    ])
                    ->setFormData($info) // 设置表单数据
                    ->hideBtn('back')
                    ->addBtn('<button type="button" class="btn btn-default" onclick="back()">返回</button>')
                    ->setExtraJs($js)
                    ->fetch();
                break;
            case 'tab2':
                $member_id = session('id');
                $member_band = Db::name('member_settlement')->where('memberid',$member_id)->find();
                // 保存数据
                if ($this->request->isPost()) {
                    $data = $this->request->post();

                    // 验证
                    $result = $this->validate($data, 'Bank');
                    // 验证失败 输出错误信息
                    if(true !== $result){
                        return $this->error($result);
                    }else {
                        if($member_band){
                            $data['updatetime']=time();
                            $res_bank = Db::name('member_settlement')->where('memberid', $member_id)->update($data);
                            if ($res_bank) {
                                $details = "所属模块('member'),操作日志('会员银行信息编辑'),会员id('$member_id')";
                                action_log('memberindust_edit', 'member', $member_id, UID, $details);
                                return $this->success('编辑成功');
                            } else {
                                return $this->error('编辑失败');
                            }
                        }else{
                            $data['memberid']=$member_id;
                            $data['createtime']=time();
                            $data['updatetime']=time();
                            $ress=Db::name('member_settlement')->insert($data);
                            if ($ress) {
                                $details = "所属模块('member'),操作日志('会员银行信息编辑'),会员id('$member_id')";
                                action_log('memberindust_edit', 'member', $member_id, UID, $details);
                                unset($_SESSION['id']);
                                return $this->success('新增成功', cookie('__forward__'));
                            } else {
                                return $this->error('新增失败');
                            }
                        }

                    }
                }

                return ZBuilder::make('form')
                    ->setTabNav($list_tab,  $group)
                    ->setPageTitle('编辑会员银行信息') // 设置页面标题
                    ->addFormItems([
                        ['text', 'accountname', '银行开户名', ''],
                        ['text', 'bankno', '公司银行账户', ''],
                        ['text', 'bankname', '开户银行支行名称', ''],
                        ['text', 'bankid', '支行联行号', ''],
                        ['text', 'bandaddr', '开户银行所在地', ''],
                    ])
                    ->layout(['accountname'=>4,'bankno'=>4,'bankname'=>4,'bankid'=>4,'bandaddr'=>4])
                    ->setFormData($member_band) // 设置表单数据
                    ->hideBtn('back')
                    ->addBtn('<button type="button" class="btn btn-default" onclick="back()">返回</button>')
                    ->setExtraJs($js)
                    ->fetch();
                break;
            case 'tab3':
                $id=session('id');
                $info = Db::name('member')->where('id',$id)->find();
                return ZBuilder::make('form')
                    ->setTabNav($list_tab,  $group)
                    ->setPageTitle('信用额度信息') // 设置页面标题
                    ->addFormItems([ // 批量添加表单项
                        ['static', 'pay_points', '消费积分', ''],
                        ['static', 'blance', '会员余额', ''],
                        ['static', 'creditlimit', '信用额度'],
                        ['static', 'temporary_creditlimit', '临时信用额度'],
                        ['static', 'account_period', '账期'],
                        ['static', 'temporary_time', '临时信用额度开始时间'],
                        ['static', 'temporary_endtime', '临时信用额度结束时间'],
                    ])
                    ->layout(['pay_points'=>3,'blance'=>3,'creditlimit'=>3,'temporary_creditlimit'=>3,'account_period'=>3,'temporary_time'=>3,'temporary_endtime'=>3])
                    ->setFormData($info) // 设置表单数据
                    ->hideBtn('submit,back')
                    ->addBtn('<button type="button" class="btn btn-default" onclick="back()">返回</button>')
                    ->setExtraJs($js)
                    ->fetch();
                break;
            case 'tab4':
                $id=session('id');
                $info = Db::name('business_license')->where('memberid',$id)->find();
                // 保存数据
                if ($this->request->isPost()) {
                    $data = $this->request->post();
                    // 验证
                    $result = $this->validate($data, 'Business');
                    // 验证失败 输出错误信息
                    if(true !== $result){
                        return $this->error($result);
                    }else{
                       if($info){
                           $data['updatetime']=time();
                           $where['memberid']=$id;
                           $where['typeid']=0;
                           $res=Db::name('business_license')->where($where)->update($data);
                           if($res){
                               return $this->success('更新成功');
                           }else{
                               return $this->error('更新失败');
                           }
                       }else{
                           $data['typeid']=0;
                           $data['createtime']=time();
                           $data['updatetime']=time();
                           $data['memberid']=$id;
                           $res=Db::name('business_license')->insert($data);
                           if($res){
                               return $this->success('更新成功');
                           }else{
                               return $this->error('更新失败');
                           }
                       }
                    }
                }
                return ZBuilder::make('form')
                    ->setTabNav($list_tab,  $group)
                    ->setPageTitle('证照信息') // 设置页面标题
                    ->addFormItems([ // 批量添加表单项
                        ['text', 'name', '法人代表姓名'],
                        ['image', 'cardimg','法人代表身份证复印件'],
                        ['image', 'shoppersoncodeimg','负责人身份证复印件'],
                        ['static','',''],
                        ['text', 'businesslicenseno', '营业执照号', ''],
                        ['date', 'effectivetime', '营业执照有效期',''],
                        ['image', 'effectiveimg', '营业执照副本图片'],
                        ['static','',''],
                        ['text', 'registeredcapital', '注册资本', ''],
                        ['text', 'organizationcode', '组织机构代码'],
                        ['image', 'organizationcodeimg','组织机构代码证复印件'],
                        ['static','',''],
                        ['text', 'generaltaxpayerproof', '一般纳税人说明'],
                        ['text', 'taxcode', '纳税类型码'],
                        ['text', 'taxno', '税务登记号码'],
                        ['image', 'taximg', '税务登记证复印件'],
                        ['textarea', 'scope', '经营范围', '']
                    ])
                    ->layout(['businesslicenseno'=>3,'effectivetime'=>3,'registeredcapital'=>3,
                        'organizationcode'=>3,'generaltaxpayerproof'=>3,'name'=>3,'taxcode'=>3,'taxno'=>3,'effectiveimg'=>3,'taximg'=>3,'organizationcodeimg'=>3,'shoppersoncodeimg'=>3,'cardimg'=>3
                    ])
                    ->setFormData($info) // 设置表单数据
                    ->hideBtn('back')
                    ->addBtn('<button type="button" class="btn btn-default" onclick="back()">返回</button>')
                    ->setExtraJs($js)
                    ->fetch();
                break;
            case 'tab5':
                $member_id = session('id');
                if ($member_id === null) return $this->error('缺少参数');
                $where['memberid']=$member_id;
                $where['typeid']=2;
                $member_band = Db::name('member_balance')->where($where)->find();
                $invoiceType=Db::name('invoice_type')->where('status=1')->column('invoice_id,invoice_type');
                $list_province = db("admin_area")->where("pid=0")->column("id,name");
                if(!empty($member_band['provinceid'])){
                    $list_city = db("admin_area")->where("pid",$member_band['provinceid'])->column("id,name");
                    $list_area = db("admin_area")->where("pid",$member_band['cityid'])->column("id,name");
                }else{
                    $list_city ='';
                    $list_area ='';
                }
                if(!empty($member_band['c_provinceid'])){
                    $c_cityid = db("admin_area")->where("pid",$member_band['c_provinceid'])->column("id,name");
                    $c_areaid = db("admin_area")->where("pid",$member_band['c_cityid'])->column("id,name");
                }else{
                    $c_cityid ='';
                    $c_areaid ='';
                }

                // 保存数据
                if ($this->request->isPost()) {
                    $data = $this->request->post();

                    // 验证
                    $result = $this->validate($data, 'Balance.update');
                    // 验证失败 输出错误信息
                    if(true !== $result){
                        return $this->error($result);
                    }else {
                        $c_province=Db::name("admin_area")->where("id=".$data['c_provinceid'])->find();
                        $c_city=Db::name("admin_area")->where("id=".$data['c_cityid'])->find();
                        if(!empty($data['c_townid'])){
                            $c_town=Db::name("admin_area")->where("id=".$data['c_townid'])->find();
                        }
                        if(empty($data['bandaddr'])){
                            if(empty($c_town['name'])){
                                $data['bandaddr']=$c_province['name'].$c_city['name'];
                            }else{
                                $data['bandaddr']=$c_province['name'].$c_city['name'].$c_town['name'];
                            }
                        }else{
                            if(strpos($data['bandaddr'],'省') || strpos($data['bandaddr'],'市')){
                                $data['bandaddr']=$data['bandaddr'];
                            }else{
                                if(empty($c_town['name'])){
                                    $data['bandaddr']=$c_province['name'].$c_city['name'].$data['bandaddr'];
                                }else{
                                    $data['bandaddr']=$c_province['name'].$c_city['name'].$c_town['name'].$data['bandaddr'];
                                }
                            }
                        }
                        $res_province=Db::name("admin_area")->where("id=".$data['provinceid'])->find();
                        $res_city=Db::name("admin_area")->where("id=".$data['cityid'])->find();
                        if(!empty($data['townid'])){
                            $res_town=Db::name("admin_area")->where("id=".$data['townid'])->find();
                        }
                        if(empty($data['signup_address'])){
                            if(empty($res_town['name'])){
                                $data['signup_address']=$res_province['name'].$res_city['name'];
                            }else{
                                $data['signup_address']=$res_province['name'].$res_city['name'].$res_town['name'];
                            }
                        }else{
                            if(strpos($data['signup_address'],'省') || strpos($data['signup_address'],'市')){
                                $data['signup_address']=$data['signup_address'];
                            }else{
                                if(empty($res_town['name'])){
                                    $data['signup_address']=$res_province['name'].$res_city['name'].$data['signup_address'];
                                }else{
                                    $data['signup_address']=$res_province['name'].$res_city['name'].$res_town['name'].$data['signup_address'];
                                }
                            }
                        }
                        if($member_band){
                            $data['updatetime']=time();
                            $res_bank = Db::name('member_balance')->where('memberid', $member_id)->update($data);
                            if ($res_bank) {
                                return $this->success('编辑成功', cookie('__forward__'));
                                unset($_SESSION['id']);
                            } else {
                                return $this->error('编辑失败');
                            }
                        }else{
                            $data['memberid']=$member_id;
                            $data['typeid']=2;
                            $data['createtime']=time();
                            $data['updatetime']=time();
                            $ress=Db::name('member_balance')->insert($data);
                            if ($ress) {
                                unset($_SESSION['id']);
                                return $this->success('新增成功', cookie('__forward__'));
                            } else {
                                return $this->error('新增失败');
                            }
                        }

                    }
                }
                return ZBuilder::make('form')
                    ->setTabNav($list_tab,  $group)
                    ->setPageTitle('编辑开票信息') // 设置页面标题
                    ->addFormItems([ // 批量添加表单项
                        ['hidden','memberid'],
                        ['select', 'invoice_type', '发票类型','',$invoiceType],
                        ['text', 'company', '单位名称', ''],
                        ['text', 'taxpayer_no', '纳税人识别号', ''],
                        ['static', '', ''],
                        ['image', 'taxpayerimg','一般纳税人资质'],
                        ['image', 'addticketempowerimg','增票授权委托书'],
                        ['image', 'businesschangeimg','工商变更证明照'],
                        ['static', '', ''],
                        ['text', 'tel', '电话', ''],
                        ['text', 'accountname', '银行开户名', ''],
                        ['text', 'bankno', '公司银行账户',''],
                        ['text', 'bankname', '开户银行支行名称', ''],
                        ['text', 'bankid', '支行联行号', ''],
                        ['static', '', ''],
                        ['linkage', 'c_provinceid', '选择省份','',$list_province,'',url('c_city'),'c_cityid,c_townid'],
                        ['linkage', 'c_cityid', '选择城市','',$c_cityid,'',url('c_area'),'c_townid'],
                        ['select', 'c_townid', '选择区域','',$c_areaid],
                        ['text', 'bandaddr', '开户银行所在地', ''],
                        ['linkage', 'provinceid', '选择省份','',$list_province,'',url('get_city'),'cityid,townid'],
                        ['linkage', 'cityid', '选择城市','',$list_city,'',url('get_area'),'townid'],
                        ['select', 'townid', '选择区域','',$list_area],
                        ['text', 'signup_address', '注册地址', ''],
                    ])
                    ->layout(['accountname'=>3,'bankno'=>3,'bankname'=>3,'bankid'=>3,'bandaddr'=>3,'invoice_type'=>3,'taxpayerimg'=>3,'addticketempowerimg'=>3,'businesschangeimg'=>3,
                        'company'=>3,'taxpayer_no'=>3,'tel'=>3,'provinceid'=>3,'cityid'=>3,'townid'=>3,'signup_address'=>3,'c_provinceid'=>3,'c_cityid'=>3,'c_townid'=>3])
                    ->setFormData($member_band) // 设置表单数据
                    ->hideBtn('back')
                    ->addBtn('<button type="button" class="btn btn-default" onclick="back()">返回</button>')
                    ->setExtraJs($js)
                    ->fetch();
                break;
        }

    }
    //查看会员信息
    public function look($id='',$group = 'tab1'){
        $url=cookie('__forward__');
        $js = <<<EOF
            <script type="text/javascript">
              function back(){
              window.location.href="$url";
              }
            </script>
EOF;
        $list_tab = [
            'tab1' => ['title' => '会员信息', 'url' => url('look', ['group' => 'tab1'])],
            'tab3' => ['title' => '信用额度', 'url' => url('look', ['group' => 'tab3'])],
            'tab4' => ['title' => '证照信息', 'url' => url('look', ['group' => 'tab4'])],
//            'tab2' => ['title' => '银行信息', 'url' => url('look', ['group' => 'tab2'])],
            'tab5' => ['title' => '开票信息', 'url' => url('look', ['group' => 'tab5'])],
        ];
        session('id',$id);
        switch ($group) {
            case 'tab1':
                $id = session('id');
                // 获取数据(排除password)
                $where['id']=$id;
                $info = Db::name('member')->where($where)->field('password', true)->find();
                $address=getArea($info['provinceid'],$info['cityid'],$info['townid']);
                $info['address']=$address;
                $sex=MemberStatusModel::getMembersex($info['sex']);
                $info['sex']=$sex;
                $depot=MemberStatusModel::getMemberDepot($info['is_depot']);
                $info['is_depot']=$depot;
                $status=MemberStatusModel::getStatusAttr($info['status']);
                $info['status']=$status;
                $is_enable=MemberStatusModel::getMemberIsEnable($info['is_enable']);
                $info['is_enable']=$is_enable;
                $level = Db::name('member_level')->where('id',$info['member_level'])->field('name')->find();
                $info['member_level']=$level['name'];
                $industry = Db::name('member_industry')->where('id',$info['industry'])->field('name')->find();
                $info['industry']=$industry['name'];
                $company_nature=Db::name('company_nature')->where('id',$info['company_nature'])->field('company_nature')->find();
                $info['company_nature']=$company_nature['company_nature'];
                $department=Db::name('member_department')->where('department_id',$info['department'])->field('department')->find();
                $info['department']=$department['department'];
                return ZBuilder::make('form')
                    ->setTabNav($list_tab,  $group)
                    ->addFormItems([ // 批量添加表单项
                        ['image', 'head', '会员头像', ''],
                        ['image', 'idcard_face', '身份证正面照', ''],
                        ['image', 'idcard_opposite', '身份证反面照', ''],
                        ['image', 'hand_photo', '身份证手持照', ''],
                        ['static', '', ''],
                        ['static', 'user_name', '用户名', ''],
                        ['static', 'mobile', '手机号'],
                        ['static', 'name', '联系人', ''],
                        ['static', 'l_mobile', '联系人手机号'],
                        ['static', 'email', '邮箱', ''],
                        ['static', 'sex', '性别', '',],
                        ['static', 'tel', '固话'],
                        ['static', 'qq', 'qq'],
                        ['static', 'wechat', '微信号'],
                        ['static', 'birthday', '生日'],
                        ['static', 'id_type', '证件类型', ''],
                        ['static', 'id_no', '证件号码'],
                        ['static', 'member_level', '会员等级','',],
                        ['static', '', ''],
                        ['static', 'company', '公司名称'],
                        ['static', 'company_nature', '公司性质','',$company_nature],
                        ['static', 'industry', '公司行业','',$industry],
                        ['static', 'department', '所在部门','',$department],
                        ['static', 'is_depot', '有无仓库','',$depot],
                        ['static', '', '', ''],
                        ['static', 'address', '地区', ''],
                        ['static', 'addr', '详细地址'],
                        ['static', 'create_time', '添加日期'],
                        ['static', 'update_time', '更新日期'],
                        ['static', '', '', ''],
                        ['static', 'status', '审核状态', ''],
                        ['static', 'is_enable', '是否启用', '']
                    ])
                    ->layout(['user_name'=>3,'name'=>3,'email'=>3,'sex'=>3,'head'=>3,'idcard_face'=>3,'idcard_opposite'=>3,'hand_photo'=>3,
                        'tel'=>3,'password'=>3,'mobile'=>3,'qq'=>3,'wechat'=>3,'birthday'=>3,'id_type'=>3,'id_no'=>3,'member_level'=>3,
                        'address'=>5,'addr'=>7,'company'=>3,'industry'=>3,'create_time'=>3,'update_time'=>3,'status'=>3,'is_enable'=>3,
                        'company_nature'=>3,'department'=>3,'l_mobile'=>3
                    ])
                    ->setFormData($info) // 设置表单数据
                    ->hideBtn('submit,back')
                    ->addBtn('<button type="button" class="btn btn-default" onclick="back()">返回</button>')
                    ->setExtraJs($js)
                    ->fetch();
                break;
            case 'tab2':
                $member_id = session('id');
                $member_band = Db::name('member_settlement')->where('memberid',$member_id)->select();
                if($member_band){
                    foreach ($member_band as $k=>$value){
                        $create=$value['createtime'];
                        $update=$value['updatetime'];
                        $value['createtime']=date("Y-m-d H:i:s",$create);
                        $value['updatetime']=date("Y-m-d H:i:s",$update);
                        $params[]= ['static', $value['accountname'], '银行开户名', ''];
                        $params[]= ['static', $value['bankno'], '公司银行账户', ''];
                        $params[]= ['static', $value['bankname'], '开户银行支行名称', ''];
                        $params[]= ['static', $value['bankid'], '支行联行号', ''];
                        $params[]= ['static', $value['bandaddr'], '开户银行所在地', ''];
                        $params[]= ['static', $value['createtime'], '添加日期', ''];
                        $params[]= ['static', $value['updatetime'], '更新日期', ''];
                        $params[] = ['static','',''];
                    }

                }
                $lays = [];
                foreach ($params as $key => $value) {
                    $lays = $lays + [$value['1'] => 3];
                }
                return ZBuilder::make('form')
                    ->setTabNav($list_tab,  $group)
                    ->setPageTitle('银行信息') // 设置页面标题
                    ->addFormItems($params)
                    ->layout($lays)
                    ->setFormData($member_band) // 设置表单数据
                    ->hideBtn('submit,back')
                    ->addBtn('<button type="button" class="btn btn-default" onclick="back()">返回</button>')
                    ->setExtraJs($js)
                    ->fetch();
                break;
            case 'tab3':
                $id=session('id');
                $info = Db::name('member')->where('id',$id)->find();
                return ZBuilder::make('form')
                    ->setTabNav($list_tab,  $group)
                    ->setPageTitle('信用额度信息') // 设置页面标题
                    ->addFormItems([ // 批量添加表单项
                        ['static', 'pay_points', '消费积分', ''],
                        ['static', 'blance', '会员余额', ''],
                        ['static', 'creditlimit', '信用额度'],
                        ['static', 'temporary_creditlimit', '临时信用额度'],
                        ['static', 'account_period', '账期'],
                        ['static', 'temporary_time', '临时信用额度开始时间'],
                        ['static', 'temporary_endtime', '临时信用额度结束时间'],
                    ])
                    ->layout(['pay_points'=>3,'blance'=>3,'creditlimit'=>3,'temporary_creditlimit'=>3,'account_period'=>3,'temporary_time'=>3,'temporary_endtime'=>3])
                    ->setFormData($info) // 设置表单数据
                    ->hideBtn('submit,back')
                    ->addBtn('<button type="button" class="btn btn-default" onclick="back()">返回</button>')
                    ->setExtraJs($js)
                    ->fetch();
                break;
            case 'tab4':
                $id=session('id');
                $where['memberid']=$id;
                $where['typeid']=0;
                $info = Db::name('business_license')->where($where)->find();
                return ZBuilder::make('form')
                    ->setTabNav($list_tab,  $group)
                    ->setPageTitle('证照信息') // 设置页面标题
                    ->addFormItems([ // 批量添加表单项
                        ['static', 'name', '法人代表姓名'],
                        ['image', 'cardimg','法人代表身份证复印件'],
                        ['image', 'shoppersoncodeimg','负责人身份证复印件'],
                        ['static','',''],
                        ['static', 'businesslicenseno', '营业执照号', ''],
                        ['static', 'effectivetime', '营业执照有效期',''],
                        ['image', 'effectiveimg', '营业执照副本图片'],
                        ['static','',''],
                        ['static', 'registeredcapital', '注册资本', ''],
                        ['static', 'organizationcode', '组织机构代码'],
                        ['image', 'organizationcodeimg','组织机构代码证复印件'],
                        ['static','',''],
                        ['static', 'generaltaxpayerproof', '一般纳税人说明'],
                        ['static', 'taxcode', '纳税类型码'],
                        ['static', 'taxno', '税务登记号码'],
                        ['image', 'taximg', '税务登记证复印件'],
                        ['static', 'scope', '经营范围', '']
                    ])
                    ->layout(['businesslicenseno'=>3,'effectivetime'=>3,'registeredcapital'=>3,
                        'organizationcode'=>3,'generaltaxpayerproof'=>3,'name'=>3,'taxcode'=>3,'taxno'=>3,'effectiveimg'=>3,'taximg'=>3,'organizationcodeimg'=>3,'shoppersoncodeimg'=>3,'cardimg'=>3
                    ])
                    ->setFormData($info) // 设置表单数据
                    ->hideBtn('submit,back')
                    ->addBtn('<button type="button" class="btn btn-default" onclick="back()">返回</button>')
                    ->setExtraJs($js)
                    ->fetch();
                break;
            case 'tab5':
                $member_id = session('id');
                $where['memberid']=$member_id;
                $where['typeid']=2;
                $member_band = Db::name('member_balance')->where($where)->find();
                $map['status']=1;
                $map['invoice_id']=$member_band['invoice_type'];
                $invoiceType=Db::name('invoice_type')->where($map)->field('invoice_type')->find();
                $member_band['invoice_type']=$invoiceType['invoice_type'];
                if($member_band){
                    $create=$member_band['createtime'];
                    $update=$member_band['updatetime'];
                    $member_band['createtime']=date("Y-m-d H:i:s",$create);
                    $member_band['updatetime']=date("Y-m-d H:i:s",$update);
                }

                return ZBuilder::make('form')
                    ->setTabNav($list_tab,  $group)
                    ->setPageTitle('开票信息') // 设置页面标题
                    ->addFormItems([ // 批量添加表单项
                        ['static', 'invoice_type', '发票类型',],
                        ['static', 'company', '单位名称', ''],
                        ['static', 'taxpayer_no', '纳税人识别号', ''],
                        ['static', '', ''],
                        ['image', 'taxpayerimg','一般纳税人资质'],
                        ['image', 'addticketempowerimg','增票授权委托书'],
                        ['image', 'businesschangeimg','工商变更证明照'],
                        ['static', '', ''],
                        ['static', 'tel', '电话', ''],
                        ['static', 'accountname', '银行开户名', ''],
                        ['static', 'bankno', '公司银行账户',''],
                        ['static', 'bankname', '开户银行支行名称', ''],
                        ['static', 'bankid', '支行联行号', ''],
                        ['static', 'bandaddr', '开户银行所在地', ''],
                        ['static', 'signup_address', '注册地址', ''],
                        ['static', '', ''],
                        ['static', 'createtime', '添加日期'],
                        ['static', 'updatetime', '更新日期'],
                    ])
                    ->layout(['accountname'=>3,'bankno'=>3,'bankname'=>3,'bankid'=>3,'bandaddr'=>4,'invoice_type'=>3,
                        'company'=>3,'taxpayer_no'=>3,'tel'=>3,'signup_address'=>4,'createtime'=>3,'updatetime'=>3,
                    'taxpayerimg'=>3,'addticketempowerimg'=>3,'businesschangeimg'=>3])
                    ->setFormData($member_band) // 设置表单数据
                    ->hideBtn('submit,back')
                    ->addBtn('<button type="button" class="btn btn-default" onclick="back()">返回</button>')
                    ->setExtraJs($js)
                    ->fetch();
                break;
        }
    }
    public function get_city($provinceid = '') {
        $result =Db::name("admin_area")->where("pid=$provinceid")->field("id,name")->select();
        $list=[];
        foreach ($result as $key => $value) {
            # code...
            $list[$key]['key']=$value['id'];
            $list[$key]['value']=$value['name'];
        }

        $arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
        $arr['list'] = $list; //数据
        return json($arr);
    }

    public function get_area($cityid = '') {
        $result =Db::name("admin_area")->where("pid=$cityid")->field("id,name")->select();
        $list=[];
        foreach ($result as $key => $value) {
            # code...
            $list[$key]['key']=$value['id'];
            $list[$key]['value']=$value['name'];
        }

        $arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
        $arr['list'] = $list; //数据
        return json($arr);
    }
    public function c_city($c_provinceid = '') {
        $result =Db::name("admin_area")->where("pid=$c_provinceid")->field("id,name")->select();
        $list=[];
        foreach ($result as $key => $value) {
            # code...
            $list[$key]['key']=$value['id'];
            $list[$key]['value']=$value['name'];
        }

        $arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
        $arr['list'] = $list; //数据
        return json($arr);
    }

    public function c_area($c_cityid = '') {
        $result =Db::name("admin_area")->where("pid=$c_cityid")->field("id,name")->select();
        $list=[];
        foreach ($result as $key => $value) {
            # code...
            $list[$key]['key']=$value['id'];
            $list[$key]['value']=$value['name'];
        }

        $arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
        $arr['list'] = $list; //数据
        return json($arr);
    }
    //批量审核禁用会员
    public function disable($record = [])
    {
        $ids=input('');
        $ids=implode(',',$ids['ids']);
        $data['is_enable']=0;
        $details = "所属模块('member'),操作日志('批量审核禁用会员'),会员id('$ids')";
        action_log('member_disable', 'member', $ids, UID, $details);
        $this->auditing($data);

    }
    //批量审核启用用会员
    public function isenable()
    {
        $data['is_enable']=1;
        $this->auditing($data);

    }
    public function auditing($data){
        $ids= $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        foreach ($ids as $values){
            $res=Db::name("member")->where('id',$values)->update($data);
        }
        if ($res) {
            return $this->success('操作成功',cookie('__forward__'));
        } else {
            return $this->error('操作失败');
        }
    }
    //批量删除银行信息
    public function deletebanks(){
        $ids= $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        foreach ($ids as $values){
            $res=Db::name("member_settlement")->where('id',$values)->delete();
        }
        if ($res) {
            return $this->success('操作成功',cookie('__bank__'));
        } else {
            return $this->error('操作失败');
        }
    }
    //删除银行
    public function deletebank($id=''){
        $res=Db::name("member_settlement")->where('id',$id)->delete();
        if ($res) {
            return $this->success('操作成功',cookie('__bank__'));
        } else {
            return $this->error('操作失败');
        }
    }
    //导出
    public function export(){
        $data = $this->request->get();
        $where['member.id'] = array('in',$data['ids']);
        $xlsName  = "member";
        $xlsCell  = array(
            ['id', 'id'],
            ['name', '会员名称'],
            ['levelname', '会员等级'],
            ['mobile', '手机号'],
            ['email', '邮箱'],
            ['sex', '性别'],
            ['birthday', '生日'],
            ['qq', 'qq'],
            ['wechat', '微信号'],
            ['id_no', '证件号码'],
            ['id_type', '证件类型'],
            ['tel', '固话'],
            ['provinceid', '省份编号'],
            ['province', '省份'],
            ['cityid', '城市编号'],
            ['city', '城市'],
            ['townid', '区域编号'],
            ['town', '区域'],
            ['addr', '详细地址'],
            ['company', '公司名称'],
            ['indname', '公司行业'],
            ['cn', '公司性质'],
            ['dname', '所在部门'],
            ['pay_points', '消费积分'],
            ['blance', '会员余额'],
            ['expand1', '扩展字段1'],
            ['expand2', '扩展字段2'],
            ['expand3', '扩展字段3'],
            ['shopid', '店铺id'],
            ['creditlimit', '信用额度'],
            ['temporary_creditlimit', '临时信用额度'],
            ['account_period', '账期'],
            ['temporary_time', '临时信用额度开始时间'],
            ['temporary_endtime', '临时信用额度结束时间'],
            ['status', '会员状态'],
            ['is_enable', '是否启用']
        );
        $count = MemberModel::view('member', true)
            ->view('member_level','name levelname','member_level.id=member.member_level','left')
            ->view('member_industry','name indname','member_industry.id=member.industry','left')
            ->view('company_nature','company_nature cn','company_nature.id=member.company_nature','left')
            ->view('member_department','department_id,department dname','department_id=member.department','left')
            ->where($where)
            ->count();
        $filename = 'Member'.date('_YmdHis');
        $xlsData  = [];
        $i = 0;
        $p = 1;
        do{
            $xlsData = MemberModel::exportSelect($where,'member.id desc',$p);
            $this->exportExcel($xlsName,$xlsCell,$xlsData);
            $p += 1;
            $i += 1000;
            unset($xlsData);
        }while($i<= $count);

        $dbpath = "./public/uploads/exportzip/";
        $zippath = $dbpath;

        $bakup=$filename;
        //zip文件名
        $z=new \util\PHPZip(); //新建立一个zip的类
        $z->ZipAndDownload($dbpath."/".$bakup,$bakup); //添加指定目录
    }


    public function exportExcel($expTitle,$expCellName,$expTableData){
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = 'Member'.date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        import('util.PHPExcel', EXTEND_PATH, '.class.php');
        $objPHPExcel = new \PHPExcel();
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
        // $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
        }
        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<$dataNum;$i++){
            for($j=0;$j<$cellNum;$j++){
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), " ".$expTableData[$i][$expCellName[$j][0]]);
            }
        }

        if(!file_exists($_SERVER['DOCUMENT_ROOT'] . "/public/uploads/exportzip/".$fileName."/")){
            mkdir($_SERVER['DOCUMENT_ROOT'] . "/public/uploads/exportzip/".$fileName."/",0777);
            chmod($_SERVER['DOCUMENT_ROOT'] . "/public/uploads/exportzip/".$fileName."/",0777);
        }
        $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
        $objWriter->save($_SERVER['DOCUMENT_ROOT']."/public/uploads/exportzip/".$fileName."/" . $xlsTitle.'.xlsx');
        /*header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;   */
    }

    //下载模板
    public function downloadexcel() {
        $file=$_SERVER['DOCUMENT_ROOT']. "/public/uploads/template/member.xlsx";
        if(is_file($file)) {
            header("Content-Type: application/force-download");
            header("Content-Disposition: attachment; filename=".basename($file));
            readfile($file);
            exit;
        }else{
            $this->error('文件不存在！');
        }
    }
    //导入
    public function memberimport(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if(empty($data['excelfile'])){
                $this->error('请上传文件');
            }
            $file = Db::name('admin_attachment')->where('id', $data['excelfile'])->value('path');
            $filetmpname = $_SERVER['DOCUMENT_ROOT'] . '/public/' . $file;

            import('util.PHPExcel', EXTEND_PATH, '.class.php');
            $PHPExcel = new \PHPExcel();
            $PHPReader = new \PHPExcel_Reader_Excel2007();
            if(!$PHPReader->canRead($filetmpname)){
                $PHPReader = new \PHPExcel_Reader_Excel5();
                if(!$PHPReader->canRead($filetmpname)){
                    $PHPReader = new \PHPExcel_Reader_CSV();
                    if(!$PHPReader->canRead($filetmpname)){
                        $this->error('文档解析错误');
                    }
                }
            }
            $PHPExcel = $PHPReader->load($filetmpname);
            $freight=array();
            $sheet = $PHPExcel->getSheet(0);
            $highestRow = $sheet->getHighestRow();
            $strOut="";
            $ctime=date('Y-m-d H:i:s');
            for ($row = 2; $row <= $highestRow; $row++){
                $name=$sheet->getCell('A'.$row)->getValue();
                if(empty($name)){
                    continue;
                }
                $psd=$sheet->getCell('B'.$row)->getValue();
                $psd2=crypt($psd,'CRYPT_MD5 ');
                $mobile=db('member')->where(array('mobile'=>$sheet->getCell('E'.$row)->getValue()))->find();
                if($mobile){
                    $strOut.="第:{$row}行信息导入失败：错误信息是手机号码已存在\r\n";
                    continue;
                }
                $tel=db('member')->where(array('tel'=>$sheet->getCell('F'.$row)->getValue()))->find();
                if($tel){
                    $strOut.="第:{$row}行信息导入失败：错误信息是固话已存在\r\n";
                    continue;
                }
                $email=db('member')->where(array('email'=>$sheet->getCell('G'.$row)->getValue()))->find();
                if($email){
                    $strOut.="第:{$row}行信息导入失败：错误信息是邮箱已存在\r\n";
                    continue;
                }
                $time=$sheet->getCell('I'.$row)->getValue();
                $birthday=str_replace('.','-',$time);
                $id_no=db('member')->where(array('id_no'=>$sheet->getCell('M'.$row)->getValue()))->find();
                if($id_no){
                    $strOut.="第:{$row}行信息导入失败：错误信息是证件号码已存在\r\n";
                    continue;
                }
                $member_level=db('member_level')->where(array('name'=>$sheet->getCell('D'.$row)->getValue()))->find();
                if(!$member_level){
                    $strOut.="第:{$row}行信息导入失败：错误信息是会员等级不存在\r\n";
                    continue;
                }
                $type=db('member_idtype')->where(array('typename'=>'证件类型'))->find();
                $values=explode(',',$type['paper']);
                $id_type=$sheet->getCell('L'.$row)->getValue();
                $res=in_array($id_type,$values);
                if(!$res){
                    $strOut.="第:{$row}行信息导入失败：错误信息是证件类型不存在\r\n";
                    continue;
                }
                $industry=db('member_industry')->where(array('name'=>$sheet->getCell('R'.$row)->getValue()))->find();
                if(!$industry){
                    $strOut.="第:{$row}行信息导入失败：错误信息是公司行业不存在\r\n";
                    continue;
                }
                $department=db('member_department')->where(array('department'=>$sheet->getCell('S'.$row)->getValue()))->find();
                if(!$department){
                    $strOut.="第:{$row}行信息导入失败：错误信息是会员部门不存在\r\n";
                    continue;
                }
                $company_nature=db('company_nature')->where(array('company_nature'=>$sheet->getCell('T'.$row)->getValue()))->find();
                if(!$company_nature){
                    $strOut.="第:{$row}行信息导入失败：错误信息是公司性质不存在\r\n";
                    continue;
                }
                $province=db('admin_area')->where(array('name'=>$sheet->getCell('N'.$row)->getValue()))->find();
                if(!$province){
                    $strOut.="第:{$row}行信息导入失败：错误信息是省/市信息不存在\r\n";
                    continue;
                }else{
                    $city=db('admin_area')->where(array('name'=>$sheet->getCell('O'.$row)->getValue(),'pid'=>$province['id']))->find();
                    if(!$city){
                        $strOut.="第:{$row}行信息导入失败：错误信息是市/区信息不存在\r\n";
                        continue;
                    }else{
                        $town=db('admin_area')->where(array('name'=>$sheet->getCell('P'.$row)->getValue(),'pid'=>$city['id']))->find();
                        if(!$town){
                            $strOut.="第:{$row}行信息导入失败：错误信息是区/县信息不存在\r\n";
                            continue;
                        }
                    }
                }


                $member_freight=db('member')
                    ->where(array(
                        'company'=>$sheet->getCell('C'.$row)->getValue(),
                    ))
                    ->find();
                if(!$member_freight){
                    if($sheet->getCell('H'.$row)->getValue()=='男'){
                        $freight=array(
                            'name'=>$sheet->getCell('A'.$row)->getValue(),
                            'sex'=>0,
                            'password'=>$psd2,
                            'company'=>$sheet->getCell('C'.$row)->getValue(),
                            'member_level'=>$member_level['id'],
                            'mobile'=>$sheet->getCell('E'.$row)->getValue(),
                            'tel'=>$sheet->getCell('F'.$row)->getValue(),
                            'email'=>$sheet->getCell('G'.$row)->getValue(),
                            'birthday'=>$birthday,
                            'qq'=>$sheet->getCell('J'.$row)->getValue(),
                            'wechat'=>$sheet->getCell('K'.$row)->getValue(),
                            'id_type'=>$sheet->getCell('L'.$row)->getValue(),
                            'id_no'=>$sheet->getCell('M'.$row)->getValue(),
                            'addr'=>$sheet->getCell('Q'.$row)->getValue(),
                            'province'=>$sheet->getCell('N'.$row)->getValue(),
                            'city'=>$sheet->getCell('O'.$row)->getValue(),
                            'town'=>$sheet->getCell('P'.$row)->getValue(),
                            'provinceid'=>$province['id'],
                            'cityid'=>$city['id'],
                            'townid'=>$town['id'],
                            'industry'=>$industry['id'],
                            'department'=>$department['department_id'],
                            'company_nature'=>$company_nature['id'],
                            'create_time'=>$ctime,
                            'update_time'=>$ctime,
                        );
                    }
                    if($sheet->getCell('H'.$row)->getValue()=='女'){
                        $freight=array(
                            'name'=>$sheet->getCell('A'.$row)->getValue(),
                            'sex'=>1,
                            'password'=>$psd2,
                            'company'=>$sheet->getCell('C'.$row)->getValue(),
                            'member_level'=>$member_level['id'],
                            'mobile'=>$sheet->getCell('E'.$row)->getValue(),
                            'tel'=>$sheet->getCell('F'.$row)->getValue(),
                            'email'=>$sheet->getCell('G'.$row)->getValue(),
                            'birthday'=>$birthday,
                            'qq'=>$sheet->getCell('J'.$row)->getValue(),
                            'wechat'=>$sheet->getCell('K'.$row)->getValue(),
                            'id_type'=>$sheet->getCell('L'.$row)->getValue(),
                            'id_no'=>$sheet->getCell('M'.$row)->getValue(),
                            'addr'=>$sheet->getCell('Q'.$row)->getValue(),
                            'province'=>$sheet->getCell('N'.$row)->getValue(),
                            'city'=>$sheet->getCell('O'.$row)->getValue(),
                            'town'=>$sheet->getCell('P'.$row)->getValue(),
                            'provinceid'=>$province['id'],
                            'cityid'=>$city['id'],
                            'townid'=>$town['id'],
                            'industry'=>$industry['id'],
                            'department'=>$department['department_id'],
                            'company_nature'=>$company_nature['id'],
                            'create_time'=>$ctime,
                            'update_time'=>$ctime,
                        );
                    }
                    $aid=db("member")->insert($freight);
                }else{
                    if($sheet->getCell('H'.$row)->getValue()=='男'){
                        $freight=array(
                            'name'=>$sheet->getCell('A'.$row)->getValue(),
                            'password'=>$psd2,
                            'sex'=>0,
                            'member_level'=>$member_level['id'],
                            'mobile'=>$sheet->getCell('E'.$row)->getValue(),
                            'tel'=>$sheet->getCell('F'.$row)->getValue(),
                            'email'=>$sheet->getCell('G'.$row)->getValue(),
                            'birthday'=>$birthday,
                            'qq'=>$sheet->getCell('J'.$row)->getValue(),
                            'wechat'=>$sheet->getCell('K'.$row)->getValue(),
                            'id_type'=>$sheet->getCell('L'.$row)->getValue(),
                            'id_no'=>$sheet->getCell('M'.$row)->getValue(),
                            'addr'=>$sheet->getCell('Q'.$row)->getValue(),
                            'provinceid'=>$province['id'],
                            'cityid'=>$city['id'],
                            'townid'=>$town['id'],
                            'industry'=>$industry['id'],
                            'department'=>$department['department_id'],
                            'company_nature'=>$company_nature['id'],
                            'update_time'=>$ctime,
                        );
                    }
                    if($sheet->getCell('H'.$row)->getValue()=='女'){
                        $freight=array(
                            'name'=>$sheet->getCell('A'.$row)->getValue(),
                            'password'=>$psd2,
                            'sex'=>1,
                            'member_level'=>$member_level['id'],
                            'mobile'=>$sheet->getCell('E'.$row)->getValue(),
                            'tel'=>$sheet->getCell('F'.$row)->getValue(),
                            'email'=>$sheet->getCell('G'.$row)->getValue(),
                            'birthday'=>$birthday,
                            'qq'=>$sheet->getCell('J'.$row)->getValue(),
                            'wechat'=>$sheet->getCell('K'.$row)->getValue(),
                            'id_type'=>$sheet->getCell('L'.$row)->getValue(),
                            'id_no'=>$sheet->getCell('M'.$row)->getValue(),
                            'addr'=>$sheet->getCell('Q'.$row)->getValue(),
                            'provinceid'=>$province['id'],
                            'cityid'=>$city['id'],
                            'townid'=>$town['id'],
                            'industry'=>$industry['id'],
                            'department'=>$department['department_id'],
                            'company_nature'=>$company_nature['id'],
                            'update_time'=>$ctime,
                        );
                    }
                    $aid=db("member")->where(array('company'=>$sheet->getCell('C'.$row)->getValue()))->update($freight);
                }
                if (!$aid) {
                    $errormsg=D("user")->getError();
                    $strOut.="第:{$row}行信息导入失败：错误信息是{$errormsg}\r\n";
                }
            }

            if($strOut!=""){
                $this->error($strOut);
            }else{
                $this->success('导入成功', null, ['_parent_reload'=>1]);
            }

        }

        return ZBuilder::make('form')
            ->addFormItems([ // 批量添加列
                ['importfile', 'excelfile'],
            ])
            ->fetch();
    }

    //选择会员列表
    public function memberlist(){
        $search=input('q');
        $where['name']=['like',"%$search%"];
        $where['status']=1;
        $member=Db::name('member')->field('id,name')->where($where)->select();
        $memberlist=[];
        $a= 0;
        foreach($member as $value){
            $b = $a++;
            $key=$value['id'];
            $memberlist[$b]['id']=$key;
            $memberlist[$b]['name']=$value['name'];
        }
        return json(['items'=>$memberlist,'total_count'=>sizeof($memberlist)]);
    }

}